package cc.ufinity.networkdiagnose.manager;

import android.content.Context;
import android.graphics.Bitmap;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import cc.ufinity.networkdiagnose.util.HttpUtils;

/**
 * HTTP页面访问类
 * 
 * @author ZR
 * 
 */
public final class HttpTestManager extends AbstractManager {

	private HttpTest httpTest;
	private String address;
	public long totalDelayTime;
	private long pageLength;
	private long start;
	public int currentTime;
	public long delayTime;
	public boolean processing;
	public int failedTimes;
	private int frequency;

	public long getPageLength() {
		return pageLength;
	}

	public String getAddress() {
		return address;
	}

	double[] xdata = null;

	public double[] getXdata() {
		return xdata;
	}

	double[] ydata = null;

	public double[] getYdata() {
		return ydata;
	}

	public HttpTestManager(Context ctx) {
		httpTest = new HttpTest(ctx);
	}

	public HttpTest getHttpTest() {
		return httpTest;
	}

	public long getDelayTime() {
		return delayTime;
	}

	public int getCurrentTime() {
		return currentTime;
	}

	public void startHttpTest(final String url, final int frequency) {
		this.frequency = frequency;
		failedTimes = 0;
		xdata = new double[frequency];
		ydata = new double[frequency];
		if (!url.startsWith("http://")) {
			address = "http://" + url;
		} else {
			address = url;
		}
		while (currentTime < frequency) {
			if (!processing) {
				processing = true;
				load();
			}
		}
	}

	private void load() {
		start = System.currentTimeMillis();
		currentTime++;
		pageLength = HttpUtils.getContentLength(address);
		if (pageLength > 0) {
			delayTime = System.currentTimeMillis() - start;
			totalDelayTime += delayTime;
			sendMsg(MessageWhat.HTTP_PAGELOAD_SUCCESS);
		} else {
			sendMsg(MessageWhat.HTTP_PAGELOAD_FAILED);
		}
		xdata[currentTime - 1] = currentTime;
		ydata[currentTime - 1] = (double) delayTime / 1000;
		if (currentTime == frequency) {
			sendMsg(MessageWhat.DONE);
		}
	}

	@Deprecated
	final class HttpTest {
		private long start;
		private WebView webView;
		private Context ctx;

		private int frequency;
		private int currentTime = 0;
		private String url;

		private long delayTime;

		public HttpTest(Context ctx) {
			this.ctx = ctx;
			// this.httpTestHandler = httpTestHandler;
			webView = new WebView(ctx);
		}

		public long getDelayTime() {
			return delayTime;
		}

		public int getCurrentTime() {
			return currentTime;
		}

		public void start(final String url, int frequency) {
			this.url = url;
			this.frequency = frequency;
			loadWeb();
		}

		private void clearCache(WebView wv) {
			wv.clearCache(true);
			ctx.deleteDatabase("webview.db");
			ctx.deleteDatabase("webviewCache.db");

		}

		private void loadWeb() {
			// if (reload) {
			// webView.reload();
			// } else {
			webView.setWebViewClient(new TestWebViewClient());
			webView.getSettings().setAllowFileAccess(true);
			webView.getSettings().setAppCacheEnabled(false);
			// webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
			webView.getSettings().setJavaScriptEnabled(true);
			webView.loadUrl(url);
			// reload = true;
			// }
		}

		final class TestWebViewClient extends WebViewClient {

			public boolean shouldOverrideUrlLoading(final WebView view,
					final String url) {
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				start = System.currentTimeMillis();
				super.onPageStarted(view, url, favicon);

			}

			@Override
			public void onPageFinished(WebView view, final String url) {
				super.onPageFinished(view, url);
				delayTime = System.currentTimeMillis() - start;
				// setTotalDelayTime(delayTime);
				xdata[currentTime] = currentTime;
				ydata[currentTime] = delayTime / 1000;// 以秒为单位
				currentTime++;
				// 定义页面加载完成消息，通知界面刷新数据
				sendMsg(MessageWhat.HTTP_PAGELOAD_SUCCESS);
				if (currentTime < frequency) {
					clearCache(webView);
					loadWeb();
				} else {
					// reload = false;
					clearCache(webView);
					// 完成HTTP访问测试，通知界面绘制图形报表
					sendMsg(MessageWhat.DONE);
				}
			}
		}
	}

}
